<html>

<head>
  <meta charset="utf-8">
  <title>菜鸟教程(runoob.com)</title>
</head>

<body>
  <h1>PHP 表单处理</h1>
  <form action="welcome.php" method="post">
    名字: <input type="text" name="fname">
    年龄: <input type="text" name="age">
    <input type="submit" value="提交">
  </form>
  <h1>PHP 获取下拉菜单的数据</h1>
  <?php
  $q = isset($_GET['q']) ? htmlspecialchars($_GET['q']) : '';
  if ($q) {
    if ($q == 'RUNOOB') {
      echo '菜鸟教程<br>http://www.runoob.com';
    } else if ($q == 'GOOGLE') {
      echo 'Google 搜索<br>http://www.google.com';
    } else if ($q == 'TAOBAO') {
      echo '淘宝<br>http://www.taobao.com';
    }
  } else {
  ?>
    <form action="" method="get">
      <select name="q">
        <option value="">选择一个站点:</option>
        <option value="RUNOOB">Runoob</option>
        <option value="GOOGLE">Google</option>
        <option value="TAOBAO">Taobao</option>
      </select>
      <input type="submit" value="提交">
    </form>
  <?php
  }
  ?>
  <h1>PHP 下拉菜单多选</h1>
  <?php
  $q = isset($_POST['q']) ? $_POST['q'] : '';
  if (is_array($q)) {
    $sites = array(
      'RUNOOB' => '菜鸟教程: http://www.runoob.com',
      'GOOGLE' => 'Google 搜索: http://www.google.com',
      'TAOBAO' => '淘宝: http://www.taobao.com',
    );
    foreach ($q as $val) {
      // PHP_EOL 为常量，用于换行
      echo $sites[$val] . PHP_EOL;
    }
  } else {
  ?>
    <form action="" method="post">
      <select multiple="multiple" name="q[]">
        <option value="">选择一个站点:</option>
        <option value="RUNOOB">Runoob</option>
        <option value="GOOGLE">Google</option>
        <option value="TAOBAO">Taobao</option>
      </select>
      <input type="submit" value="提交">
    </form>
  <?php
  }
  ?>

  <h1>单选按钮表单</h1>
  <?php
  $q = isset($_GET['q']) ? htmlspecialchars($_GET['q']) : '';
  if ($q) {
    if ($q == 'RUNOOB') {
      echo '菜鸟教程<br>http://www.runoob.com';
    } else if ($q == 'GOOGLE') {
      echo 'Google 搜索<br>http://www.google.com';
    } else if ($q == 'TAOBAO') {
      echo '淘宝<br>http://www.taobao.com';
    }
  } else {
  ?><form action="" method="get">
      <input type="radio" name="q" value="RUNOOB" />Runoob
      <input type="radio" name="q" value="GOOGLE" />Google
      <input type="radio" name="q" value="TAOBAO" />Taobao
      <input type="submit" value="提交">
    </form>
  <?php
  }
  ?>

  <h1>checkbox 复选框</h1>
  <?php
  $q = isset($_POST['q']) ? $_POST['q'] : '';
  if (is_array($q)) {
    $sites = array(
      'RUNOOB' => '菜鸟教程: http://www.runoob.com',
      'GOOGLE' => 'Google 搜索: http://www.google.com',
      'TAOBAO' => '淘宝: http://www.taobao.com',
    );
    foreach ($q as $val) {
      echo $sites[$val] . PHP_EOL;
    }
  } else {
  ?>
    <form action="" method="post">
      <input type="checkbox" name="q[]" value="RUNOOB"> Runoob<br>
      <input type="checkbox" name="q[]" value="GOOGLE"> Google<br>
      <input type="checkbox" name="q[]" value="TAOBAO"> Taobao<br>
      <input type="submit" value="提交">
    </form>
  <?php
  }
  ?>


</body>

</html>